﻿package db;

import php.Lib;

class ChannelManager extends php.db.Manager<Channel> {
	
	public function new( classval : Class<php.db.Object> ) {
		super(classval);
	}	
	
	public static function getchan(cat:Array<String>) 
	{
		var temp = 0;
		var sql = "SELECT DISTINCT ch.* FROM categories c, channeltags ct, channels ch WHERE (";
		for (s in cat) {
		sql =  sql +"c.name = '" + s + "' ";
		temp++;
		if (temp < cat.length) sql += "OR ";
		
		}
		sql = sql + " ) AND c.id=ct.id_cat AND ct.id_ch=ch.id;";
		return (sql);
	}
	
	public static function getch(id:Int)
	{
		var sql = "SELECT ch.* FROM channeltags ct, channels ch WHERE ct.id_cat=" + id + " AND ct.id_ch=ch.id;";
		return (sql);
	}
	
	public static function geti(id:Int)
	{
		var sql = "SELECT i.* FROM itemtags it, items i WHERE it.id_cat=" + id + " AND it.id_item=i.id;";
		return (sql);
	}
	
	public static function strtoarr(s:String) {
		var l = s.length;
		var ct: Array<String>;
		var st = '';
		var p = 0 ;
		ct = [];
		for (i in 0...l+1) {
			if ((s.charAt(i) == ',') || (i==l)) { ct.insert(p, st); p++; st = ''; }
			else { st = st + s.charAt(i); }
		}
	return ct;
    }	
	public static function GetByName (name: String) { 
		var can: List <Channel> = Channel.manager.search( { name: name }, false);
		var can1: Channel = can.first();
		return can1;
	}
}